首页>代码>ssh开发OA办公后台管理系统>/oa - 副本/src/com/bjsxt/oa/manager/impl/AbstractManager.java
package com.bjsxt.oa.manager.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bjsxt.oa.PagerModel;
import com.bjsxt.oa.SystemContext;
import com.bjsxt.oa.manager.SystemException;

public class AbstractManager extends HibernateDaoSupport {
	public PagerModel searchPaginated(String hql){
		return searchPaginated(hql, null);
	}
	
	public PagerModel searchPaginated(String hql,Object param){
		return searchPaginated(hql, new Object[]{param});
	}
	
	public PagerModel searchPaginated(String hql,Object[] params){
		return searchPaginated(hql, params,SystemContext.getOffset(), SystemContext.getPagesize());
	}
	
	public PagerModel searchPaginated(String hql,int offset,int pagesize){
		return searchPaginated(hql,null, offset, pagesize);
	}
	public PagerModel searchPaginated(String hql,Object param,int offset,int pagesize){
		return searchPaginated(hql, new Object[]{param}, offset, pagesize);
	}
	public PagerModel searchPaginated(String hql,Object[] params,int offset,int pagesize){
		//获取记录总数
		String countHql = getCountQuery(hql);
		Query query = getSession().createQuery(countHql);
		if(params != null && params.length > 0){
			for(int i=0; i<params.length; i++){
				query.setParameter(i, params[i]);
			}
		}
		int total = ((Long)query.uniqueResult()).intValue();
		
		//获取结果集
		query = getSession().createQuery(hql);
		if(params != null && params.length > 0){
			for(int i=0; i<params.length; i++){
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult(offset);
		query.setMaxResults(pagesize);
		List datas = query.list();
		
		//返回PagerModel
		PagerModel pm = new PagerModel();
		pm.setDatas(datas);
		pm.setTotal(total);
		return pm;
	}
	
	/**
	 * 根据HQL语句,获得查询总记录数的HQL语句
	 * 如:
	 * select ... from Organization o where o.parent is null
	 * 经过转换,可以得到:
	 * select count(*) from Organziation o where o.parent is null
	 * @param hql
	 * @return
	 */
	private String getCountQuery(String hql){
		int index = hql.indexOf("from");
		if(index != -1){
			return "select count(*) " + hql.substring(index);
		}
		throw new SystemException("无效的HQL查询语句【"+hql+"】");
	}
}
最近下载更多
cx123123  LV7 2022年5月18日
and123456  LV11 2022年2月7日
luk7153  LV3 2021年9月24日
huaua7676  LV30 2021年7月25日
17852310375  LV3 2021年4月27日
ayanamirei  LV3 2021年4月21日
shiyujir  LV7 2021年4月7日
zh17826038126  LV2 2021年1月4日
过去的事刚擦手感不错  LV2 2020年10月13日
ddqddqddq  LV14 2020年9月5日
最近浏览更多
vluobo  LV1 3月19日
Gin19960217  LV4 3月5日
1134116035 2月21日
暂无贡献等级
氟西汀来救你  LV2 1月9日
WBelong  LV7 2023年12月25日
dazhuang123 2023年10月7日
暂无贡献等级
fesfefe  LV13 2023年9月26日
2036495585  LV9 2023年9月18日
carloscarlos  LV1 2023年9月14日
8战魂5无双8  LV43 2023年8月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友